package com.yuri.demo.route

import androidx.compose.runtime.Composable
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.yuri.demo.ui.page.PageOneScreen
import com.yuri.demo.ui.page.PageThreeScreen
import com.yuri.demo.ui.page.PageTwoScreen

@Composable
fun AppNavHost() {
    val navController = rememberNavController()

    NavHost(
        navController = navController,
        startDestination = RouteConfig.PAGE_ONE
    ) {
        composable(RouteConfig.PAGE_ONE) {
            PageOneScreen(navController)
        }
        composable("${RouteConfig.PAGE_TWO}/{${ParamConfig.NAME}}/{${ParamConfig.AGE}}",
            arguments = listOf(
                navArgument(ParamConfig.NAME) {
                    type = NavType.StringType
                },
                navArgument(ParamConfig.AGE) {
                    type = NavType.IntType
                    defaultValue = 22
                    nullable = false
                }
            )
            ) {
            //通过composable函数中提供的NavBackStackEntry提取参数
            val arg = requireNotNull(it.arguments)
            val name = arg.getString(ParamConfig.NAME)
            val age = arg.getInt(ParamConfig.AGE)
            PageTwoScreen(name, age, navController)
        }
        composable(RouteConfig.PAGE_THREE) {
            PageThreeScreen(navController)
        }
    }
}